<?php
/**
 * Created by jinqiang on 2025/1/2 9:32.
 * Copyright (c) 2025, Your Company. All rights reserved.
 */


namespace App\Http\Services\Callback;


use App\Http\Models\Mysql\SsyBnInvoiceListHongchongTable;
use App\Http\Models\Mysql\SsyBnInvoiceListHuiTable;
use App\Http\Models\Mysql\SsyBnInvoiceListKpLogTable;
use App\Http\Models\Mysql\SsyBnInvoiceListTable;
use App\Http\Repository\BanniuRepository;

class NNuoService
{
    public function invoice($param)
    {
        $contentParam = json_decode($param['content'], true);
        //判断是否冲红
        if(isset($contentParam['c_hjje']) && $contentParam['c_hjje'] < 0){
            $hongchongObj = (new SsyBnInvoiceListHongchongTable())->where('sourceid', $contentParam['c_orderno'])->first();
            if(!empty($hongchongObj)){
                (new SsyBnInvoiceListTable())->updateDataWhere(['sourceid' => $contentParam['c_orderno']], [
                    'invoicing_status' => 4,//开票状态 默认0未开票 1已开票 2开票失败  3申请冲红 4冲红成功  5冲红失败
                    'hongchong_status' => $hongchongObj['hongchong_status']
                ]);
            }
        }
        $data = [
            "terminalNumber" => $contentParam['terminalNumber'] ?? null,
            "c_hjje"         => $contentParam['c_hjje'] ?? null,
            "c_cjsj"         => $contentParam['c_cjsj'] ?? null,
            "c_saletaxnum"   => $contentParam['c_saletaxnum'] ?? null,
            "c_payee"        => $contentParam['c_payee'] ?? null,
            "c_salerTel"     => $contentParam['c_salerTel'] ?? null,
            "c_remark"       => $contentParam['c_remark'] ?? null,
            "cipherText"     => isset($contentParam['cipherText']) ? serialize($contentParam['cipherText']) : null,
            "qrCode"         => $contentParam['qrCode'] ?? null,
            "c_invoice_line" => $contentParam['c_invoice_line'] ?? null,
            "c_paper_pdf_url"=> $contentParam['c_paper_pdf_url'] ?? null,
            "c_ofd_url"      => $contentParam['c_ofd_url'] ?? null,
            "c_qdbz"         => $contentParam['c_qdbz'] ?? null,
            "c_salerAccount" => $contentParam['c_salerAccount'] ?? null,
            "c_kprq"         => $contentParam['c_kprq'] ?? null,
            "c_status"       => $contentParam['c_status'] ?? null,
            "c_checker"      => $contentParam['c_checker'] ?? null,
            "specificFactor" => $contentParam['specificFactor'] ?? null,
            "email"          => $contentParam['email'] ?? null,
            "c_orderno"      => $contentParam['c_orderno'] ?? null,
            "bankAccount"    => $contentParam['bankAccount'] ?? null,
            "address"        => $contentParam['address'] ?? null,
            "c_imgUrls"      => $contentParam['c_imgUrls'] ?? null,
            "machineCode"    => $contentParam['machineCode'] ?? null,
            "c_jpg_url"      => $contentParam['c_jpg_url'] ?? null,
            "telephone"      => $contentParam['telephone'] ?? null,
            "c_hjse"         => $contentParam['c_hjse'] ?? null,
            "c_clerkId"      => $contentParam['c_clerkId'] ?? null,
            "c_qdxmmc"       => $contentParam['c_qdxmmc'] ?? null,
            "c_salerAddress" => $contentParam['c_salerAddress'] ?? null,
            "checkCode"      => $contentParam['checkCode'] ?? null,
            "c_deptId"       => $contentParam['c_deptId'] ?? null,
            "c_fphm"         => $contentParam['c_fphm'] ?? null,
            "c_clerk"        => $contentParam['c_clerk'] ?? null,
            "c_url"          => $contentParam['c_url'] ?? null,
            "invoiceItems"   => isset($contentParam['invoiceItems']) ? serialize($contentParam['invoiceItems']) : null,
            "c_fpdm"         => $contentParam['c_fpdm'] ?? null,
            "c_fpqqlsh"      => $contentParam['c_fpqqlsh'] ?? null,
            "phone"          => $contentParam['phone'] ?? null,
            "productOilFlag" => $contentParam['productOilFlag'] ?? null,
            "extensionNumber"=> $contentParam['extensionNumber'] ?? null,
            "c_bhsje"        => $contentParam['c_bhsje'] ?? null,
            "taxnum"         => $contentParam['taxnum'] ?? null,
            "c_salerName"    => $contentParam['c_salerName'] ?? null,
            "buyername"      => $contentParam['buyername'] ?? null,
            "dt_invoicedate" => $contentParam['dt_invoicedate'] ?? null,
            "c_gxsj"         => $contentParam['c_gxsj'] ?? null,
            "createtime" => date("Y-m-d H:i:s")
        ];
        (new SsyBnInvoiceListHuiTable())->createData($data);
        //更新班牛数据状态
        $kpLogObj = (new SsyBnInvoiceListKpLogTable())->where('order_no', $contentParam['c_orderno'])->first();
        if(!empty($kpLogObj)){
            $ids = unserialize($kpLogObj->sourceidarr);
            $list = (new SsyBnInvoiceListTable())->whereIn('sourceid', is_array($ids) ? $ids : [$ids])->get(['taskid as task_id'])->toArray();
            foreach ($list as $item){
                $res = BanniuRepository::quickUpdate($item['task_id']);
                info("通知班牛修改状态", [$item['task_id'], $res]);
                if($res['response']['code'] == 0){
                    (new SsyBnInvoiceListTable())->updateDataWhere(['taskid' => $item['task_id']], [
                        'b_status' => str_starts_with($res['response']['map']['result'][$item['task_id']], "成功") ? 1 : 2
                    ]);
                }
            }
        }
    }
}
